const { JSDOM } = require("jsdom");

module.exports = getContent;

function getContent(url) {
  return JSDOM.fromURL(url).then((dom) => {
    let document = dom.window.document;
    let story = document.querySelector(".story");

    let excludes = [
      "iframe",
      "script",
      "input",
      "button",
      ".podcast-tools",
      "#mainContent",
      ".story-meta",
      ".slug-wrap",
      ".story-tools",
      ".ad-header",
      ".ad-footer",
      ".bucketwrap.internallink",
      ".bucketwrap.statichtml",
      '[aria-label="advertisement"]',
      "[data-newsletter]",
      ".enlarge_measure",
      ".enlarge_html",
      ".enlarge-options",
      "picture > source",
      "img:not([src], [src=''])", // 移除没有指定地址的图片
      ".share-tools",
      "#end-of-story-recommendations-mount",
      ".tags",
      ".callout-end-of-story-mount-piano-wrap",
    ];
    excludes.forEach((selector) =>
      story.querySelectorAll(selector).forEach((e) => e.remove())
    );

    story.querySelectorAll(".credit-caption").forEach((div) => {
      let p = div.querySelector("p");
      if (p) div.innerHTML = "<i>" + p.firstChild.textContent + "</i>";
    });

    // 移除链接的href，防止误触跳转
    // story.querySelectorAll("a").forEach((a) => {
    //   let u = document.createElement("u");
    //   u.textContent = a.textContent;
    //   a.replaceWith(u);
    // });

    story.querySelectorAll("picture").forEach((pic) => {
      let img = pic.querySelector("img");
      if (img) {
        pic.parentElement.insertAdjacentElement(
          "beforeend",
          img.cloneNode(true)
        );
      }
      pic.remove();
    });

    let title = story.querySelector(".storytitle > h1").textContent;

    story.querySelector(".storytitle").innerHTML = "<h3>" + title + "</h3>";

    story.querySelectorAll("[class], [id], [style]").forEach((e) => {
      e.removeAttribute("class");
      e.removeAttribute("id");
      e.removeAttribute("style");
    });

    let data = story.outerHTML.replace(/<!--[\s\S]*?-->/g, "");

    console.log(title);

    return {
      title,
      data,
    };
  });
}
